using Moq;
using NUnit.Framework;
using Tycho.Parser.Peg;

namespace Tycho.Parser.Tests.Peg {
    [TestFixture]
    public class RuleProductionSpecification {
        [Test]
        public void ShouldParseIfRuleParses () {
            var context = new ParseContext ();
            ParseResult result = ParseRuleProduction (context, new RuleParseResult (7, context));
            Assert.That (result, Is.Not.Null);
            Assert.That (result.Index, Is.EqualTo (7));
            Assert.That (result.Term, Is.Null);
            Assert.That (result.Context, Is.SameAs (context));
        }

        [Test]
        public void ShouldNotParseIfRuleDoesntParse () {
            var context = new ParseContext ();
            ParseResult result = ParseRuleProduction (context, null);
            Assert.That (result, Is.Null);
        }

        private static ParseResult ParseRuleProduction (ParseContext context, RuleParseResult ruleResult) {
            var rule = new Mock<IRule> ();

            const string sourceString = "stuff";
            char[] source = sourceString.ToCharArray ();
            int index = 0;
            var environment = new ParseEnvironment ("filename");

            rule.Setup (r => r.Parse (source, index, context, sourceString, environment))
                .Returns (ruleResult);

            var ruleProduction = new RuleProduction (rule.Object);

            return ruleProduction.Parse (source, index, context, sourceString, environment);
        }
    }
}